;;; This file is part of guix-bavier.git
;;; Copyright © 2020 Eric Bavier <bavier@posteo.net>
;;; License: GPLv3+

(define-module (bavier packages emulators)
  #:use-module (guix build-system gnu)
  #:use-module (guix git-download)
  #:use-module (guix licenses)
  #:use-module (bavier licenses)
  #:use-module (guix packages)
  #:use-module (gnu packages curl)
  #:use-module (gnu packages electronics)
  #:use-module (gnu packages sdl)
  #:use-module (gnu packages wxwidgets))

;;; TODO: See if we can read data files directly from the store, rather than
;;; importing them into the users home directory.
(define-public emma-02
  (package
   (name "emma_02")
   (version "1.36")
   (source (origin
            (method git-fetch)
            (uri (git-reference
                  (url "https://github.com/etxmato/emma_02")
                  (commit "4f2fb6d99aab8a8126a3e8ba58f94d0a5e0445fb")))
            (file-name (git-file-name name version))
            (sha256
             (base32
              "0kifjirq9fps43gvagw7z3z0zbizcp0bsvma85wlxcsblsnlwa0f"))))
   (build-system gnu-build-system)
   (inputs
    `(("curl" ,curl)
      ("libserialport" ,libserialport)
      ("sdl" ,(sdl-union (list sdl sdl-mixer)))
      ("wxwidgets" ,wxwidgets)))
   (arguments
    `(#:tests? #f                       ; No tests
      #:modules ((ice-9 ftw)
                 ,@%gnu-build-system-modules)
      #:phases
      (modify-phases %standard-phases
        (delete 'configure)
        (replace 'build
          (lambda* (#:key inputs #:allow-other-keys)
            (for-each (lambda (v)
                        (setenv v (string-append (assoc-ref inputs "sdl")
                                                 "/include/SDL:"
                                                 (or (getenv v) ""))))
                      (list "CPLUS_INCLUDE_PATH" "C_INCLUDE_PATH"))
            (chdir "Release")
            (invoke "make")
            (chdir "..")
            #t))
        (replace 'install
          (lambda* (#:key outputs #:allow-other-keys)
            (let* ((out (assoc-ref outputs "out"))
                   (bin (string-append out "/bin"))
                   (data (string-append out "/share"))
                   (emma (string-append data "/emma_02"))
                   (doc (string-append data "/doc/emma_02"))
                   (copy (lambda (src dst)
                           (copy-recursively src (string-append dst "/" src)))))
              (install-file "Release/emma_02" bin)
              (install-file "copyright.rtf" doc)
              (install-file "introduction.rtf" doc)
              (install-file "Linux build/emma_02.desktop"
                            (string-append data "/applications"))
              (install-file "Linux build/emma02.xpm"
                            (string-append data "/pixmaps"))
              (for-each (lambda (f) (install-file f emma))
                        (scandir "." (lambda (file)
                                       ((file-name-predicate
                                         ".*\\.(syntax|def|xrc|ini|htb)")
                                        file #t))))
              (copy "data" emma)
              (copy "Configurations" emma)
              (copy "images" emma)
              (copy "images_linux" emma)
              #t))))))
   (home-page "https://www.emma02.hobby-site.com")
   (synopsis "RCA CDP1802 Multi-system Emulator")
   (description "The Emma 02 Emulator can emulate the following computers:
@itemize @bullet
@item CDP18S020 Evaluation Kit
@item Cidelsa
@item COMX-35
@item Cosmac Elf
@item Cosmac Elf 2000
@item Cosmac VIP
@item Cosmac VIP II, supporting two versions: ED and RCA
@item Cosmicos
@item ETI 660
@item FRED 1
@item FRED 1.5
@item Infinite UC1800
@item MCDS, Microboard Computer Development System
@item Membership Card
@item Microboard System, supporting CPU boards CDP18S600 to CDP18S610
@item Microtutor
@item Microtutor II
@item MS 2000, MicroDisk Development System
@item Netronics Elf II
@item Oscom Nano
@item Pecom 32 and 64
@item Quest Super Elf
@item RCA Coin Arcade
@item RCA Studio II
@item RCA Studio III / Victory MPT-02
@item RCA Studio IV
@item Telmac TMC-600
@item Telmac 1800
@item Telmac 2000
@item VELF
@item VIP2K Membership Card
@item Visicom COM-100
@end itemize")
   (license (list lgpl2.0+
                  cc-by-nc-sa3.0        ; Quest Electronics components
                  (permission-for-commercial "src/about.cpp")))))
